using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BirthdayReminder.Entities;
using BirthdayReminder.Business;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Collections;

/// <summary>
/// Summary description for RequestHandler
/// </summary>
public class GetData : IHttpHandler
{
    public GetData()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region IHttpHandler Members

    bool IHttpHandler.IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();

        string action = context.Request.Params["action"];
        switch (action)
        {
            case "GetPresentsListByReceiverId":
                {
                    string receiverID = context.Request.Params["receiverID"];
                    int ID = 0;
                    if(receiverID != null && int.TryParse(receiverID, out ID))
                    {
                        GetPresentsListByReceiverId(context, ID);
                    }
                } break;
            case "GetTotalPresentsList":
                {
                    string receiverIDList = context.Request.Params["receiverIDList"];
                    int ID = 0;
                    if (receiverIDList != string.Empty)
                    {
                        GetTotalPresentsList(context, receiverIDList);
                    }
                } break;
            case "GetIDList":
                {
                    GetIDList(context);
                } break;
        }

        context.Response.Flush();
        context.Response.End();
    }

    private void GetPresentsListByReceiverId(HttpContext context, int receiverID)
    {
        ProxyGiftCollection proxyGiftCollection = new ProxyGiftCollection();
        PresentCollection presents = PresentService.GetPresentsListByReceiverId(receiverID, PresentColumns.CreationDate, Pyco.Data.OrderDirection.ASC);
        foreach (Present present in presents)
        {
            Gift gift = GiftService.GetGift(present.GiftId);
            if (gift != null)
            {
                ProxyGift proxyGift = new ProxyGift(
                            string.Format("{0}.png", present.GiftId)
                            ,gift.Name
                            ,present.Message
                            ,gift.Price
                            ,present.SenderId
                            );
                proxyGiftCollection.Add(proxyGift);
            }

        }
        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
        js.Serialize(context.Response.Output, proxyGiftCollection);

        /*
        ProxyGiftCollection proxyGiftCollection = new ProxyGiftCollection();
        if (receiverID == 90713713)
        {
            proxyGiftCollection.Add(new ProxyGift(
                                "1.png"
                                , "Gift name 1"
                                , "Tang qua 1 ne"
                                , 100
                                , 90937965));

            proxyGiftCollection.Add(new ProxyGift(
                                "2.png"
                                , "Gift name 2"
                                , "Tang qua 2 ne"
                                , 100
                                , 90937965));
        }
        else if (receiverID == 90937965)
        {
            proxyGiftCollection.Add(new ProxyGift(
                                "2.png"
                                , "Gift name 2"
                                , "Tang qua 2 ne"
                                , 100
                                , 90713713));

            proxyGiftCollection.Add(new ProxyGift(
                                "3.png"
                                , "Gift name 3"
                                , "Tang qua 3 ne"
                                , 100
                                , 90713713));
        }

        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
        js.Serialize(context.Response.Output, proxyGiftCollection);
        */
    }

    private void GetTotalPresentsList(HttpContext context, string receiverIDList)
    {
        TotalNumberPresentCollection totalNumberPresents = PresentService.GetTotalNumberPresent(receiverIDList);
        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
        js.Serialize(context.Response.Output, totalNumberPresents);

        /*
        TotalNumberPresentCollection totalNumberPresents = new TotalNumberPresentCollection();
        TotalNumberPresent totalNumberPresent1 = new TotalNumberPresent();
        totalNumberPresent1.TotalNumber = 1;
        totalNumberPresent1.ReceiverID = 90713713;

        TotalNumberPresent totalNumberPresent2 = new TotalNumberPresent();
        totalNumberPresent2.TotalNumber = 2;
        totalNumberPresent2.ReceiverID = 90937965;

        totalNumberPresents.Add(totalNumberPresent1);
        totalNumberPresents.Add(totalNumberPresent2);

        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
        js.Serialize(context.Response.Output, totalNumberPresents);
        */
    }

    private void GetIDList(HttpContext context)
    {
        
        UserCollection users = UserService.GetUserList(UserColumns.UserId, Pyco.Data.OrderDirection.ASC);
        ArrayList arr = new ArrayList();
        foreach (User user in users)
        {
            arr.Add(user.UserId);
        }
        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
        js.Serialize(context.Response.Output, arr);
         
        /*
        ArrayList arr = new ArrayList();
        arr.Add(90713713);
        arr.Add(90937965);
        arr.Add(90539613);
        Newtonsoft.Json.JsonSerializer js = new Newtonsoft.Json.JsonSerializer();
        js.Serialize(context.Response.Output, arr);
        */
    }
    #endregion
}
